package com.hushui.mall.commodity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.common.utils.BeanUtilCopy;
import com.hushui.mall.commodity.entity.BrandEntity;
import com.hushui.mall.commodity.entity.CategoryEntity;
import com.hushui.mall.commodity.entity.vo.CategoryBrandVo;
import com.hushui.mall.commodity.entity.vo.CategorylogVo;
import com.hushui.mall.commodity.service.BrandService;
import com.hushui.mall.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hushui.mall.commodity.entity.CategoryBrandRelationEntity;
import com.hushui.mall.commodity.service.CategoryBrandRelationService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:45
 */
@RestController
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("commodity/categorybrandrelation/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/product/categorybrandrelation/brands/list")
    public R getBrand(@RequestParam Map<String, Object> params){

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        Long catId = (Long)params.get("catId");
        wrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);
        List<CategoryBrandVo> categoryBrandVos = new ArrayList<CategoryBrandVo>();
        for (CategoryBrandRelationEntity a:list
        ) {categoryBrandVos.add(new CategoryBrandVo(a.getBrandId(),a.getBrandName()));
        }
        return R.ok().put("data", categoryBrandVos);
    }
    @RequestMapping("/product/categorybrandrelation/catelog/list")
    public R getCategory(@RequestParam Map<String, Object> params){

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        Long catId = (Long)params.get("catId");
        wrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);
        List<CategorylogVo> categorylogVos = new ArrayList<CategorylogVo>();
        for (CategoryBrandRelationEntity a:list
             ) {categorylogVos.add(new CategorylogVo(a.getCatelogId(),a.getCatelogName()));
        }
        return R.ok().put("data", categorylogVos);
    }
    @RequestMapping("/product/categorybrandrelation/save")
    public R saveCategorybrandrelation(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        QueryWrapper<BrandEntity> brandEntityQueryWrapper = new QueryWrapper<BrandEntity>().eq("brand_id", categoryBrandRelation.getBrandId());
        QueryWrapper<CategoryEntity> categoryEntityQueryWrapper = new QueryWrapper<CategoryEntity>().eq("cat_id", categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandService.getOne(brandEntityQueryWrapper).getName());
        categoryBrandRelation.setCatelogName(categoryService.getOne(categoryEntityQueryWrapper).getName());
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("commodity/categorybrandrelation/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/categorybrandrelation/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/categorybrandrelation/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("commodity/categorybrandrelation/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
